package com.lingyue.entity.deserializer;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import lombok.extern.slf4j.Slf4j;


import java.io.IOException;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;

/**
 * @author admin
 */
@Slf4j
public class DateDeserializer extends JsonDeserializer<LocalDateTime> {
        private static final String TIME_SYMBOL = ":";

        @Override
        public LocalDateTime deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException, JsonProcessingException {
            if (StringUtils.isBlank(jsonParser.getText()))
                return null;
            if (jsonParser.getText().contains(TIME_SYMBOL)){
                return LocalDateTime.parse(jsonParser.getText(),DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
            }
            LocalDate localDate = LocalDate.parse(jsonParser.getText(), DateTimeFormatter.ofPattern("yyyy-MM-dd"));
            return LocalDateTime.of(localDate, LocalTime.MIN);
        }
}
